package com.alpha.processor.processor.anno_processor.base;

import com.alpha.processor.creators.info_model.ConnectorInterfaceInfoModel;
import com.alpha.processor.creators.interface_file.ProviderInterfaceFileCreator;
import com.alpha.processor.processor.anno_processor.InterfaceAnnoProcessor;
import com.alpha.processor.processor.tools.ProcessorTool;
import com.alpha.processor.creators.connector_file.ConnecteCreateModel;
import com.alpha.processor.creators.connector_file.ConnectorFileCreator;
import com.alpha.processor.processor.anno_processor.CreateConnectorAnnoProcessor;
import com.alpha.processor.processor.anno_processor.DataProcessorAnnoProcessor;
import com.alpha.processor.processor.anno_processor.OnViewClickAnnoProcessor;
import com.alpha.processor.processor.anno_processor.ViewBinderAnnoProcessor;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic;

public class FileCreateAnnoProcessor{

    // String --- className,
    // ConnectorFileCreator --- generateClassCreator
    protected Map<String, ConnectorFileCreator> connectorCreatorMap = new HashMap<>();

    private final CreateConnectorAnnoProcessor createConnectorAnnoProcessor;
    private final DataProcessorAnnoProcessor dataProcessorAnnoProcessor;
    private final ViewBinderAnnoProcessor viewBinderAnnoProcessor;
    private final OnViewClickAnnoProcessor onViewClickAnnoProcessor;

    public FileCreateAnnoProcessor(Messager messager, Elements elementUtils, ProcessorTool logger) {

        viewBinderAnnoProcessor = new ViewBinderAnnoProcessor(messager,elementUtils,logger,connectorCreatorMap);
        onViewClickAnnoProcessor = new OnViewClickAnnoProcessor(messager,elementUtils,logger,connectorCreatorMap);

        dataProcessorAnnoProcessor = new DataProcessorAnnoProcessor(messager,elementUtils,logger,connectorCreatorMap);

        createConnectorAnnoProcessor = new CreateConnectorAnnoProcessor(messager, elementUtils, logger,connectorCreatorMap);

    }

    public void processViewBinderAnno(RoundEnvironment roundEnvironment){
        viewBinderAnnoProcessor.processAnno(roundEnvironment);
    }

    public void processOnViewClickAnno(RoundEnvironment roundEnvironment){
        onViewClickAnnoProcessor.processAnno(roundEnvironment);
    }

    public void processDataProcessorAnno(RoundEnvironment roundEnvironment){
        dataProcessorAnnoProcessor.processAnno(roundEnvironment);
    }

    public void processCreateConnectorAnno(RoundEnvironment roundEnvironment){
        createConnectorAnnoProcessor.processAnno(roundEnvironment);
    }

    public void syncNecessaryDatas(Messager messager, Element clickAspectElement, Map<String, ProviderInterfaceFileCreator> providerCreatorMap,
                                   ConnectorInterfaceInfoModel interfaceInfoModel){
        messager.printMessage(Diagnostic.Kind.NOTE,"sync classes");

        //  sync ClickAspect && Provider
        for (String className:connectorCreatorMap.keySet()){
            messager.printMessage(Diagnostic.Kind.NOTE,"view :" + className);

            ConnectorFileCreator connectorFileCreator = connectorCreatorMap.get(className);
            ProviderInterfaceFileCreator providerFileCreator = providerCreatorMap.get(className);

            ConnecteCreateModel connecteCreateModel = connectorFileCreator.getCreateModel();

            connecteCreateModel.updateConnectorInterfaceInfoModel(interfaceInfoModel);
            connecteCreateModel.getViewInfoModel().updateClickAspectElement(clickAspectElement);
            connecteCreateModel.updateProviderCreateModel(providerFileCreator);
        }
    }

    public Map<String, ConnectorFileCreator> getConnectorCreatorMap() {
        return connectorCreatorMap;
    }

}
